package com.rs2hd.content;

import com.rs2hd.GameEngine;
import com.rs2hd.event.Event;
import com.rs2hd.model.Entity;
import com.rs2hd.model.NPC;
import com.rs2hd.model.Player;
import com.rs2hd.model.World;
import com.rs2hd.util.Misc;
import com.rs2hd.model.PlayerDetails;


/**
 * The death event handles player and npc deaths. Drops loot, does animation, teleportation, etc.
 *
 * @author Graham
 */
public class DeathEvent extends Event {
public Player player;
	private PlayerDetails details;

    private Entity entity;
    private boolean firstNpcStage = false;

    /**
     * Creates the death event for the specified entity.
     *
     * @param entity The player or npc whose death has just happened.
     */
    public DeathEvent(Entity entity) {
        super(3500);
        this.entity = entity;
        this.entity.resetTurnTo();
        this.entity.animate(entity.getDeathAnimation());
    }

    @Override
    public void execute() {
        if (entity instanceof NPC) {
			NPC n1 = (NPC) entity;
			n1.getCombat().setAttacking(false);
            if (!firstNpcStage) {
            	NPC n = (NPC) entity;
                entity.setHidden(true);
                n.dropLoot();
                entity.clearKillersHits();
                n.getCombat().setAttacking(false);
                n.teleport(n.getLocation());
                this.setTick(n.getDefinition().getRespawn() * 500);
                this.firstNpcStage = true;
            } else {
            	NPC n = (NPC) entity;
                n.dropLoot();
            	n.getCombat().setAttacking(false);
                entity.setHp(entity.getMaxHp());
                entity.setDead(false);
                entity.setHidden(false);
                this.stop();
            }
        } else if (entity instanceof Player) {
        	Player player = (Player) entity;
			if (player.getKiller() != null) {
				Player p2 = (Player) player.getKiller();
				p2.KC++;
				p2.PkPoints++;
                                p2.getActionSender().sendMessage("You Have Defeated " + player.getUsername() + ".");
                                p2.getActionSender().sendMessage("You Killed " + player.getUsername() + "! You Now Have " + p2.PkPoints + " PKShops Points");
                                player.getWalkingQueue().reset();
                                player.PvPDrop(p2);

			}
            player.setHp(entity.getMaxHp());
            player.setDead(false);
            player.teleport(World.getInstance().getConfiguration().getSpawnPoint());

          if (player.CantTeleZone()) {
                player.dropLoot();
              }
          if (player.Spawn()) {
                player.tele(1667 ,5703 , 0);
              }

            //player.dropLoot();
            player.clearKillersHits();
            player.setAttacking(false);
            player.getCombat().specialAmount = 1000;
            player.getCombat().refreshSpecial();
            player.getActionSender().sendMessage("Oh dear, you have died.");
            player.usingVeng = false;
            player.vengDelay = 0;
            player.vengUseDelay = 0;
			player.DC++;
            GameEngine.prayer.PrayerOff(player);
            player.getSkills().RestorePray(99);
			player.frozen = 0;
            World.getInstance().registerEvent(new Event(500) {
                @Override
                public void execute() {
                    this.stop();
                }
            });
            this.stop();
		}
	}

}
